<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

$model= $this->LoadModel('Public');
$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();

$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$dp_code.'&sign='.$sign['sign'];

if (empty($this->Param['option'])) {
    
    //待初审
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        if(!empty($this->Param['o_keyword'])){
            $where['`o_vip_name` LIKE (?)'] ='%'. $this->Param['o_vip_name'].'%' ;
        }else{
            if($member_info['u_role'] ==10){
                $where['`o_review_status` = ?'] = '未复核';
            }else{
                $where['`o_review_status` IN (?)'] = array('未复核','未通过');
            }
            $where['`o_adtime` > ?'] = 20171201;
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'order_oldinfo',array('o_review_status ASC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        
        $mode = Config::GetCommon();
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $row_msg = $model ->getOneRow('order_audit','*',array('`a_oid` =?' =>$row['o_id'],'`a_type` =?' =>'复核审核'));
                $row['o_review_user'] = $row_msg['a_uname'];
                $row['o_review_remark'] = $row_msg['a_remarks'];
                $row['o_review_date'] = $row_msg['a_dateline'];
                $row['o_adtime'] = empty($row['o_adtime']) ? '' :substr($row['o_adtime'],0,4).'-'.substr($row['o_adtime'],4,2).'-'.substr($row['o_adtime'],6,2);
                //$row['o_finishe_time'] = empty($row['o_finishe_time']) ? '' : date('Y-m-d H:i:s', $row['o_finishe_time']); //终审通过时间
                $order_car = $OrderHelper->orderCar($row['o_id']);
                $row['ot_loan_limit'] = $order_car['ot_loan_limit']; //贷款金额
                $row['o_pre_modetext'] = $mode['mode'][$row['o_pre_mode']]['mode_text']; //审核模式
                $bank_row = $BankHelper->bankRow($row['o_bank_id']);
                $row['o_bank_abb'] = $bank_row['bk_abbreviation'];
                //
                if($row['o_oldsys'] == 1){
                    $sign = $oldapi->sign(array('code' => $dp_code));
                    $row['info_url'] = $api_baseurl.'&id='.$row['o_number'];
                }else{
                    $row['info_url'] = '/index/index/option/info?oid='.$row['o_id'];
                }
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    } else {        
        echo $this->GetView('order_revolist.php');
    }
} else {
    switch($this->Param['option']){
        case 'revoinfo':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            //占位判断
//            $result = $OrderHelper->perchJudge($oid, $member_info);
//            if(!$result['success']){
//                ErrorMsg::Debug($result['msg']);
//            }
            $order = $OrderHelper->orderInfo($oid);
            $order['audit_ok'] = $OrderHelper->orderAuditfilter($order['audit_all']['ok'], '初审审核');
            $audit_type = $order['car']['ot_loan_limit']<=80000? 'A00003终审审核' : 'A00001终审审核';
            $order['final_ok'] = $OrderHelper->getFinalresult($oid,$audit_type);
            $order['review_draft'] = $OrderHelper->orderAuditfilter($order['audit_all']['draft'], '复核审核');
            $order['audit_msg'] = $model->getOneRow('order_audit','*',array('`a_oid` =?' =>$oid,'`a_type` =?' =>'复核审核'));
            $this->Assign('order', $order);
            echo $this->GetView('order_revoinfo.php');
            break;
        case 'save':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $save_type = empty($this->Param['savetype']) ? 'draft' : $this->Param['savetype'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            if($save_type == 'draft'){
                $this->Param['draft'] = true;
                if($this->Param['a_remarks'] ==''){
                    ErrorMsg::Debug('请在备注中说明不通过原因');
                }
            }  else {
                $this->Param['draft'] = false;
            }
            $this->Param['a_uid'] = $this->UserConfig['Id'];
            $result = $OrderHelper->dataRecheck($this->Param, $oid);
            if($result['success']){
                if($save_type != 'draft'){
                    $MemberHelper->saveLog($this->UserConfig, '复核审核', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                    //如果为终审通过 发送消息
                    if($result['data']['flag']){
                        $order_row = $OrderHelper->orderRow($oid);
                        $MessageHelper->sendSingle(array(
                            'm_uid' => $order_row['o_uid'],
                            'm_title' => '复核审核通过',
                            'm_name' => $order_row['o_uname'],
                            'm_content' => $order_row['o_vip_name'].'复核审核通过',
                            'm_dateline' => NOW_TIME,
                            'm_oid' => $oid,
                            'm_info' => serialize(array('status' => '终审通过', 'code' => $OrderHelper->statusChange('终审通过')))
                        ));
                    }
                }
                ErrorMsg::Debug($save_type == 'draft' ? '保存成功' : '审核成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'back': //订单退回
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $this->Assign('oid', $oid);
            echo $this->GetView('order_revoback.php');
            break;
        case 'backsave': //退回保存
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $status = empty($this->Param['status']) ? ErrorMsg::Debug('请选择退回状态') : $this->Param['status'];
            $remarks = $this->Param['remarks'];
            $v_type = 'revooprate';
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $result = $OrderHelper->orderBack($oid, $status, $remarks, array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']),$v_type,$this->Param['lock_car']);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '复核退回', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                //发送消息
                $order_row = $OrderHelper->orderRow($oid);
                $MessageHelper->sendSingle(array(
                    'm_uid' => $order_row['o_uid'],
                    'm_title' => '复核退回',
                    'm_name' => $order_row['o_uname'],
                    'm_content' => $order_row['o_vip_name'].'复核退回',
                    'm_dateline' => NOW_TIME,
                    'm_oid' => $oid,
                    'm_info' => serialize(array('status' => '复核退回', 'code' => $OrderHelper->statusChange('退回')))
                ));
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'chkout': //订单签出
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $result = $OrderHelper->orderChkout($oid, '终审', array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']));
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '审核级资料复核签出', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        default :
    }
}